package org.csu.mypetstorebackstage.service;

import org.csu.mypetstorebackstage.domain.LineItem;
import org.csu.mypetstorebackstage.domain.Order;
import org.csu.mypetstorebackstage.domain.OrderItem;
import org.csu.mypetstorebackstage.domain.OrderSales;
import org.csu.mypetstorebackstage.persistence.LineItemMapper;
import org.csu.mypetstorebackstage.persistence.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service

public class OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    LineItemMapper lineItemMapper;

    public  List<LineItem> getLineItemByOrderId(int orderId){
        return lineItemMapper.getLineItemByOrderId(orderId);
    }
    public List<Order> getOrdersByUserId (String userId){

        return  orderMapper.getOrdersByUserId(userId);
    }
    public Order getOrderByOrderId(int orderId){
        return orderMapper.getOrderByOrderId(orderId);
    }
    public void deleteOrderByOrderId(int orderId ){
        orderMapper.deleteOrderByOrderId(orderId);
    }
    public void deleteOrdersByUserId(String orderId){
        orderMapper.getOrdersByUserId(orderId);
    }
    public List<Order>getAllOrders(){
        return orderMapper.getAllOrders();
    }
    public void insertOrder(Order order){
        orderMapper.insertOrder(order);
    }

    public  boolean modifyOrder(Order order){
       return orderMapper.modifyOrder(order);

    }
    public  void updateOrderStatus(Order order){
        orderMapper.updateOrderStatus(order);
    }
    public void SendOrder(int orderId){
        orderMapper.SendOutOrder(orderId);
    }
    public List<OrderSales> getPriceList(){
        List<OrderSales> orderSalesList=new ArrayList<OrderSales>();
        List<Order> orderList=orderMapper.getAllOrders();
        OrderSales orderSales=new OrderSales();
        orderSales.setOrderDate(orderList.get(0).getOrderDate());
        orderSales.setOrderSales(orderList.get(0).getTotalPrice());
        orderSalesList.add(orderSales);
        for(int i=1;i<orderList.size();i++){
            int k=0;
            Date currentDate=orderList.get(i-1).getOrderDate();
                if(!(orderList.get(i).getOrderDate().equals(currentDate))){
                   OrderSales orderSales1=new OrderSales();
                   orderSales1.setOrderDate(orderList.get(i).getOrderDate());
                   orderSales1.setOrderSales(orderList.get(i).getTotalPrice());
                    orderSalesList.add(orderSales1);
                }
        }
        return  orderSalesList;

    }


    public List<OrderSales> getOrdersAmount(){

        List<OrderSales> orderSalesList=new ArrayList<OrderSales>();
        List<Order> orderList=orderMapper.getAllOrders();
        OrderSales orderSales=new OrderSales();
        orderSales.setOrderDate(orderList.get(0).getOrderDate());
        orderSales.setOrderAmount(1);
        orderSalesList.add(orderSales);
        for(int i=1;i<orderList.size();i++){
            Date currentDate=orderList.get(i-1).getOrderDate();

                if(!(orderList.get(i).getOrderDate().equals(currentDate))){
                    OrderSales orderSales1=new OrderSales();
                    orderSales1.setOrderDate(orderList.get(i).getOrderDate());
                    orderSales1.setOrderAmount(1);
                    orderSalesList.add(orderSales1);
                }
                else{
                    for(int k=0;k<orderSalesList.size();k++){
                        if(orderList.get(i).getOrderDate().equals(orderSalesList.get(k).getOrderDate())){

                            int temp=orderSalesList.get(k).getOrderAmount();
                            orderSalesList.get(k).setOrderAmount(++temp);
                        }
                    }

                }
        }
        return  orderSalesList;

    }
}
